package com.kiln.juxingqs.ui

import android.content.Intent
import androidx.lifecycle.Observer
import client.global.commlib.toast.toast
import com.kiln.juxingqs.BaseMiddleActivity
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.UserAddress
import com.kiln.juxingqs.databinding.ActivityApplyDeliveryBinding
import com.kiln.juxingqs.dialog.JyInputNumDialog
import com.kiln.juxingqs.ui.vm.ApplyDeliveryViewModel

/**
 *  申请提货
 */
class ApplyDeliveryActivity :
    BaseMiddleActivity<ApplyDeliveryViewModel, ActivityApplyDeliveryBinding>() {

    override fun getLayout(): Int {
        return R.layout.activity_apply_delivery
    }

    var goods_id: String? = ""
    var goods_name: String? = ""
    var deal_num: String? = ""
    var pick_type: String? = ""

    override fun initView() {
        binding?.apply {
            event = this@ApplyDeliveryActivity
            vm = viewModel

            goods_id = intent.getStringExtra("goods_id")
            goods_name = intent.getStringExtra("goods_name")
            deal_num = intent.getStringExtra("deal_num")
            pick_type = intent.getStringExtra("pick_type")//1.资产一，2.资产二，3.提货商品，4.可交易商品

//            intent.getParcelableExtra<GetDddBatchRet.Batch>("bean")?.let {
////                viewModel.deliveryBatch.value=it
//            }

            viewModel.goods_id.value = goods_id
            viewModel.pick_type.value = pick_type

        }

    }

    override fun initData() {
        viewModel.userInfo()
    }

    override fun observerUI() {
        viewModel.userRet.observe(this, {

            it?.let {
                if (it.isSuccess) {
                    it.data?.address_list?.let {
                        if (it.size > 0){
                            binding.addr = it[0]
                            viewModel.addR.value = it[0]
                        }
                    }
                } else {}
            }
        })

        viewModel.call.observe(this, Observer {
            it?.apply {
                if (isSuccess) {
                    toast("操作成功")
                    startActivity(Intent(this@ApplyDeliveryActivity, WdthActivity::class.java))

                    finish()
                } else {
                    toast(it.message)
//                    toast(it.message, it.code)
                }
            }
        })

    }

    override fun initViewModel(): ApplyDeliveryViewModel = createViewModel()

    fun selectNum() {
//        PagingBatchDeliveryDialog(
//            this@ApplyDeliveryActivity,
//            intent.getStringExtra("goods_id")!!
//        ).apply {
//            vm = viewModel
//            ok = {
//                viewModel.deliveryBatch.value = it
//            }
//        }.show()

        JyInputNumDialog(this@ApplyDeliveryActivity).apply {
            ok = { num ->
                viewModel.sNum.value = "${num}"
                dismiss()
            }
        }.show()
    }


    fun selectAddr() {
        startActivityForResult(Intent(this, AddrMangerActivity::class.java).apply {
            putExtra("select", true)
        }, 0x11)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        when (requestCode) {
            0x11 -> {
                if (resultCode == RESULT_OK) {
                    data?.let {
                        val addr = it.getSerializableExtra("address") as UserAddress.Address?
                        addr?.let {
                            binding.addr = it
                            viewModel.addR.value=it
                        }
                    }
                }
            }
        }
    }

}